// 时间处理工具函数

/**
 * 将时间字符串转换为相对时间显示
 * @param timeString - 时间字符串，格式如 "2025-01-15T10:30" 或 "2025-07-22T09:13:03"
 * @returns 相对时间字符串，如 "2小时前"、"3天前"等
 */
export function getRelativeTime(timeString: string): string {
    try {
        // 处理时间字符串，确保格式正确
        let dateString = timeString
        if (!timeString.includes("T")) {
            dateString = timeString + "T00:00:00"
        } else if (timeString.split("T")[1].split(":").length === 2) {
            // 如果只有小时和分钟，添加秒
            dateString = timeString + ":00"
        }

        const operationTime = new Date(dateString)
        const now = new Date()

        // 检查日期是否有效
        if (isNaN(operationTime.getTime())) {
            return "时间未知"
        }

        const diffInMs = now.getTime() - operationTime.getTime()
        const diffInSeconds = Math.floor(diffInMs / 1000)
        const diffInMinutes = Math.floor(diffInSeconds / 60)
        const diffInHours = Math.floor(diffInMinutes / 60)
        const diffInDays = Math.floor(diffInHours / 24)
        const diffInMonths = Math.floor(diffInDays / 30)
        const diffInYears = Math.floor(diffInDays / 365)

        if (diffInSeconds < 0) {
            return "刚刚"
        } else if (diffInSeconds < 60) {
            return `${diffInSeconds}秒前`
        } else if (diffInMinutes < 60) {
            return `${diffInMinutes}分钟前`
        } else if (diffInHours < 24) {
            return `${diffInHours}小时前`
        } else if (diffInDays < 30) {
            return `${diffInDays}天前`
        } else if (diffInMonths < 12) {
            return `${diffInMonths}个月前`
        } else {
            return `${diffInYears}年前`
        }
    } catch (error) {
        console.error("时间解析错误:", error, "原始时间:", timeString)
        return "时间解析错误"
    }
}

/**
 * 根据操作名称推断操作类型
 * @param action - 操作名称
 * @returns 操作类型
 */
export function getActionType(action: string): "mixed" | "sampling" | "input" | "export" | "other" {
    if (action.includes("混合编号") || action.includes("混样")) {
        return "mixed"
    } else if (action.includes("抽检编号") || action.includes("抽检")) {
        return "sampling"
    } else if (action.includes("录入") || action.includes("导入")) {
        return "input"
    } else if (action.includes("导出") || action.includes("下载")) {
        return "export"
    } else {
        return "other"
    }
}

// 声明 RecentActivity 和 ProcessedActivity 接口
interface RecentActivity {
    operationTime: string
    action: string
}

interface ProcessedActivity {
    operationTime: string
    action: string
    id: string
    actionType: "mixed" | "sampling" | "input" | "export" | "other"
    relativeTime: string
    operationDate: Date
}

/**
 * 处理最近活动数据
 * @param activities - 原始活动数据
 * @returns 处理后的活动数据
 */
export function processActivities(activities: RecentActivity[]): ProcessedActivity[] {
    return activities.map((activity, index) => ({
        ...activity,
        id: `activity-${index}-${activity.operationTime}`,
        actionType: getActionType(activity.action),
        relativeTime: getRelativeTime(activity.operationTime),
        operationDate: new Date(
            activity.operationTime.includes("T") ? activity.operationTime : activity.operationTime + "T00:00:00",
        ),
    }))
}
